package controllers_menu

import (
	"OPS_TEMPLATE/models"
	"OPS_TEMPLATE/util/html"
	"github.com/astaxie/beego"
	"strings"
)

type MenuController struct {
	beego.Controller
}

func (this *MenuController) URLMapping() {
	this.Mapping("Index", this.Index)
	this.Mapping("SearchPage", this.SearchPage)

}

// @router /menu/getleftmenus/?:key [post]
func (this *MenuController) Index() {
	var menu *models.Menu
	beego.Debug(this.GetString(":key"))
	key := this.GetString(":key")
	i := strings.Split(key, "_")
	id := i[len(i)-1]
	this.Data["leftmenu"] = menu.Getleftmenus(id)
	this.TplName = "common/_leftmenus.html"
}

// @router /menu/getsearchpage/?:key [get]
func (this *MenuController) SearchPage() {
	sousuo := ""
	code := this.GetString("table_code")
	this.Ctx.Request.Form.Del("table_code")
	page, _ := this.GetInt("table_page")
	this.Ctx.Request.Form.Del("table_page")
	args := this.GetString("table_args")
	this.Ctx.Request.Form.Del("table_args")
	for k, v := range this.Ctx.Request.Form {
		if strings.TrimSpace(v[0]) == "" {
			this.Ctx.Request.Form.Del(k)
		}
	}
	i := 0
	for k, v := range this.Ctx.Request.Form {
		if i != len(this.Ctx.Request.Form)-1 {
			sousuo = sousuo + " " + k + " " + "like '%" + strings.TrimSpace(v[0]) + "%' and"
		} else {
			sousuo = sousuo + " " + k + " " + "like '%" + strings.TrimSpace(v[0]) + "%'"
		}
		i = i + 1
	}
	beego.Debug(sousuo)
	this.Ctx.WriteString(util_html.GetListGrid(code, page, args, sousuo))
}
